package com.edou.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.member.dao.IMemberCompanyDao;
import com.edou.member.dto.MemberCompanyCondition;
import com.edou.member.entity.MemberCompany;
import com.edou.member.service.IMemberCompanyService;

/**
 * 会员所属企业信息
 * @creator     
 * @create-time 2016/4/25 15:00:52
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("memberCompanyServiceImpl")
@Transactional(value = "transactionManager")
public class MemberCompanyServiceImpl implements IMemberCompanyService{

	@Resource(name = "memberCompanyDaoImpl")
	private IMemberCompanyDao memberCompanyDao;

	/**
	 * 查询所有会员所属企业信息
	 */
	public List<MemberCompany> listMemberCompanyAll(){
		return memberCompanyDao.listMemberCompanyAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listMemberCompanyByPage(PageResult<MemberCompany> pageResult,MemberCompanyCondition condition){
		int rows = memberCompanyDao.listMemberCompanyByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberCompany> list = memberCompanyDao.listMemberCompanyByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 会员所属企业信息
	 */
	public MemberCompany getMemberCompanyById(String id){
		return memberCompanyDao.getMemberCompanyById(id);
	}

	/**
	 * 新增
	 * @param item 会员所属企业信息
	 */
	public void add(MemberCompany item){
		// 查询到父节点
		if(StringUtils.isNotBlank(item.getParentId())){
			MemberCompany memberCompany = memberCompanyDao.getMemberCompanyById(item.getParentId());
			if(memberCompany != null){
				memberCompany.setIsLeaf(0);
				memberCompanyDao.update(memberCompany);
			}
		}
		// 进行新增子节点
		memberCompanyDao.add(item);
	}

	/**
	 * 修改
	 * @param item 会员所属企业信息
	 */
	public void update(MemberCompany item){
		memberCompanyDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		memberCompanyDao.delByIds(ids);
	}

	public List<MemberCompany> listMemberCompanyByParentId(String pid) {
		return memberCompanyDao.listMemberCompanyByParentId(pid);
	}
	
	
	/**
	 * 根据父级id进行查找MemberCompany
	 */
	public List<MemberCompany> listRightByParentId(String pid,Integer level) {
		List<MemberCompany> memberCompanies = listRightByPidLoop(pid, level);
		return memberCompanies;
	}
	
	/**
	 * 递归得到数据权限树
	 * @param pid
	 * @param level
	 * @return
	 */
	private List<MemberCompany> listRightByPidLoop(String pid, Integer level){
		// 符合条件的权限结果集
		List<MemberCompany> memberCompanies = null;
		
		// 如果级数 > 0，说明至少1级才能查出结果
		if(level > 0){
			// 查询PID下的权限
			memberCompanies = memberCompanyDao.listMemberCompanyByParentId(pid);
			// 查过一级，级数-1
			level--;
			// 递归查询下级权限
			for(MemberCompany memberCompany : memberCompanies){
				// 逐一调当前的递归方法进行逐级下查
				List<MemberCompany> childRights = listRightByPidLoop(memberCompany.getId(), level);
				// 将子权限查询后存入权限实体中
				memberCompany.setChildRights(childRights);
			}
		}
		return memberCompanies;
	}

	/**
	 * 查询是否叶节点的数据
	 */
	public List<MemberCompany> listMemberCompanyByIsLeaf(Integer isLeaf) {
		return memberCompanyDao.listMemberCompanyByIsLeaf(isLeaf);
	}

	/**
	 * 根据名称进行模糊查找会员所属医院
	 */
	public List<MemberCompany> listMemberCompanyByName(String name) {
		return memberCompanyDao.listMemberCompanyByName(name);
	}
	
	/**
	 * 根据角色编码查询会员企业信息
	 */
	public List<MemberCompany> listMemberCompanyByRoleCode(String code,String name) {
		return memberCompanyDao.listMemberCompanyByRoleCode(code,name);
	}

	/**
	 * 根据企业名称查询
	 * @param name 名称
	 * @return 会员所属企业信息
	 */
	public MemberCompany  getMemberCompanyByName(String name){
		return memberCompanyDao.getMemberCompanyByName(name);
	}

	/**
	 * 根据会员ID查询所在企业
	 */
	public MemberCompany getMemberCompanyByMemberUserId(String memberUserId) {
		return memberCompanyDao.getMemberCompanyByMemberUserId(memberUserId);
	}

	/**
	 * 查询最新几条企业
	 */
	@Override
	public List<MemberCompany> listMemberCompanyLimit(Integer limit) {
		return memberCompanyDao.listMemberCompanyLimit(limit);
	}
}

